﻿using System.Net.Http.Headers;
using Utility.Webs.Clients;

namespace Utility.Webs
{
    public  class RestTemplate
    {
        private HttpClientHandler _httpClientHandler;

        public RestTemplate(HttpClientHandler httpClientHandler = null)
        {
            _httpClientHandler = httpClientHandler;
        }

        /// <summary>
        /// 发出Get请求
        /// </summary>
        /// <typeparam name="T">响应报文反序列类型</typeparam>
        /// <param name="url">请求路径</param>
        /// <param name="requestHeaders">请求额外的报文头信息</param>
        /// <returns></returns>
        public async Task<string> GetForStringAsync(string url, HttpRequestHeaders requestHeaders = null) 
        {
            var request = new HttpRequest<string>(HttpMethod.Get, url, _httpClientHandler);
            request.IgnoreSsl();
            return await request.ResultFromStringAsync();
        }

        /// <summary>
        /// 发出Post请求
        /// </summary>
        /// <typeparam name="T">响应报文反序列类型</typeparam>
        /// <param name="url">请求路径</param>
        /// <param name="body">请求数据，将会被json序列化后放到请求报文体中</param>
        /// <param name="requestHeaders">请求额外的报文头信息</param>
        /// <returns></returns>
        public async Task<string> PostForStringAsync(string url, object body = null, HttpRequestHeaders requestHeaders = null) 
        {
            var request = new HttpRequest<string>(HttpMethod.Post, url, _httpClientHandler);
            request.IgnoreSsl();
            request.JsonData(body);
            if (requestHeaders != null)
            {
                foreach (var header in requestHeaders)
                {
                    request.Header(header.Key, header.Value);
                }
            }
            return await request.ResultFromStringAsync();
        }

        /// <summary>
        /// 发出Get请求
        /// </summary>
        /// <typeparam name="T">响应报文反序列类型</typeparam>
        /// <param name="url">请求路径</param>
        /// <param name="requestHeaders">请求额外的报文头信息</param>
        /// <returns></returns>
        public async Task<T> GetForEntityAsync<T>(string url, HttpRequestHeaders requestHeaders = null) where T : class
        {
            var request = new HttpRequest<T>(HttpMethod.Get, url, _httpClientHandler);
            request.IgnoreSsl();
            return await request.ResultFromJsonAsync();
        }

        /// <summary>
        /// 发出Post请求
        /// </summary>
        /// <typeparam name="T">响应报文反序列类型</typeparam>
        /// <param name="url">请求路径</param>
        /// <param name="body">请求数据，将会被json序列化后放到请求报文体中</param>
        /// <param name="requestHeaders">请求额外的报文头信息</param>
        /// <returns></returns>
        public async Task<T> PostForEntityAsync<T>(string url, object body = null, HttpRequestHeaders requestHeaders = null) where T : class
        {
            var request = new HttpRequest<T>(HttpMethod.Post, url, _httpClientHandler);
            request.IgnoreSsl();
            request.JsonData(body);
            if (requestHeaders != null)
            {
                foreach (var header in requestHeaders)
                {
                    request.Header(header.Key, header.Value);
                }
            }
            return await request.ResultFromJsonAsync();
        }

        /// <summary>
        /// 发出Put请求
        /// </summary>
        /// <typeparam name="T">响应报文反序列类型</typeparam>
        /// <param name="url">请求路径</param>
        /// <param name="body">请求数据，将会被json序列化后放到请求报文体中</param>
        /// <param name="requestHeaders">请求额外的报文头信息</param>
        /// <returns></returns>
        public async Task<T> PutForEntityAsync<T>(string url, object body = null, HttpRequestHeaders requestHeaders = null) where T : class
        {
            var request = new HttpRequest<T>(HttpMethod.Put, url, _httpClientHandler);
            request.IgnoreSsl();
            request.JsonData(body);
            if (requestHeaders != null)
            {
                foreach (var header in requestHeaders)
                {
                    request.Header(header.Key, header.Value);
                }
            }
            return await request.ResultFromJsonAsync();
        }

        /// <summary>
        /// 发出Delete请求
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="requestHeaders">请求额外的报文头信息</param>
        /// <returns></returns>
        public async Task DeleteAsync(string url, HttpRequestHeaders requestHeaders = null)
        {
            await SendJsonAsync(HttpMethod.Delete, url, null, requestHeaders);
        }

        /// <summary>
        /// send XML as an asynchronous operation.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="url">The URL.</param>
        /// <param name="requestBody">The request body.</param>
        /// <param name="requestHeaders">The request headers.</param>
        /// <returns>Task&lt;System.String&gt;.</returns>
        public async Task<string> SendXMLAsync(HttpMethod method, string url, string requestBody = null, HttpRequestHeaders requestHeaders = null)
        {
            var request = new HttpRequest(method, url, _httpClientHandler);
            request.IgnoreSsl();
            request.ContentType(HttpContentType.Xml);
            if (!string.IsNullOrWhiteSpace(requestBody))
            {
                request.XmlData(requestBody);
            }

            if (requestHeaders != null)
            {
                foreach (var header in requestHeaders)
                {
                    request.Header(header.Key, header.Value);
                }
            }
            return await request.ResultAsync();
        }

        /// <summary>
        /// send as an asynchronous operation.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="url">The URL.</param>
        /// <param name="requestBody">The request body.</param>
        /// <param name="requestHeaders">The request headers.</param>
        /// <returns>Task&lt;System.String&gt;.</returns>
        public async Task<string> SendJsonAsync(HttpMethod method, string url, string requestBody = null, HttpRequestHeaders requestHeaders = null)
        {
            var request = new HttpRequest(method, url, _httpClientHandler);
            request.IgnoreSsl();
            request.ContentType(HttpContentType.Json);
            if (!string.IsNullOrWhiteSpace(requestBody))
            {
                request.XmlData(requestBody);
            }

            if (requestHeaders != null)
            {
                foreach (var header in requestHeaders)
                {
                    request.Header(header.Key, header.Value);
                }
            }
            return await request.ResultAsync();
        }
    }
}
